import Vue from 'vue'
import {
	baseURL
} from '../../common/config.js'

const tools = {};
let jump = true
// 接口前缀
tools.baseURL = baseURL
// 跳转
tools.navigate = function(path, redirect = false) {
	if (jump) {
		if (redirect) {
			jump = false
			uni.switchTab({
				url: path,
			});
			try {
				uni.setStorageSync('jumpTo', 2);
			} catch (e) {}
		} else {
			jump = false
			uni.navigateTo({
				url: path,
			});
		}
		setTimeout(() => {
			jump = true
		}, 500)

	} else {
		return
	}
}
// 手机号校验
tools.reg = function(res) {
	let reg = /^[1][1,2,3,4,5,6,7,8,9][0-9]{9}$/
	if (!reg.test(res)) {
		return false
	} else {
		return true
	}
}
tools.back = function(delta) {
	uni.navigateBack({
		delta: delta
	});
}
// 错误消息提示
tools.toast = function(res) {
	uni.showToast({
		icon: 'none',
		title: res,
		duration: 2000
	});
}
// 成功消息提示
tools.succend = function(res) {
	uni.showToast({
		title: res,
		duration: 2000
	});
}
tools.toOpenNotificationSettings = function() {
	return new Promise((resolve, reject) => {
		var isOn = undefined;
		try {
			if ('iOS' == plus.os.name) {
				var app = plus.ios.invoke('UIApplication', 'sharedApplication');
				var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
				plus.ios.invoke(app, 'openURL:', setting);
				plus.ios.deleteObject(setting);
				plus.ios.deleteObject(app);
			} else {
				var main = plus.android.runtimeMainActivity();
				var Intent = plus.android.importClass('android.content.Intent');
				var Build = plus.android.importClass("android.os.Build");
				var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
				var pkName = main.getPackageName();
				var uid = main.getApplicationInfo().plusGetAttribute("uid");
				//android 8.0引导  
				if (Build.VERSION.SDK_INT >= 26) {
					intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
				} else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0  
					intent.putExtra("app_package", pkName);
					intent.putExtra("app_uid", uid);
				} else { //(<21)其他--跳转到该应用管理的详情页  
					var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
					intent.setData(uri);
				}
				// 跳转到该应用的系统通知设置页  
				main.startActivity(intent);
			}
		} catch (e) {
			reject('exception in isTurnedOnPush!!');
		}
		resolve(isOn);
	})
}
// 时间处理 年月日
tools.dateFormatSm = function(time) {
	let date = new Date(time);
	let year = date.getFullYear();
	// 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
	let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
	let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate()
	// 拼接
	// return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
	return year + "-" + month + "-" + day;
}
// 时间处理 年月日时分秒
tools.dateFormat = function(time) {
	let date = new Date(time);
	let year = date.getFullYear();
	// 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
	let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
	let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
	let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
	let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
	let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	// 拼接
	// return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
	return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}

export default tools;